package com.od.controller;

import com.od.entity.Interview;
import com.od.service.InterviewService;
import com.od.utils.DataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.sql.Time;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("interview")
@CrossOrigin
public class InterviewController {
    @Autowired
    private InterviewService interviewService;

    @RequestMapping("allInterviews")
    @ResponseBody
    public List<Interview> allInterviews(){return interviewService.getAllInterviews();}

    @PostMapping("bySeeker")
    @ResponseBody
    public List<Map<String, Object>> bySeeker(@RequestBody Map<String, Object> interviewSeekerData){
        return interviewService.getInterviewInfoIn(interviewSeekerData.get("interviewSeeker").toString());
    }

    @PostMapping("selectByEnterpriseId")
    @ResponseBody
    public List<Map<String, Object>> selectByEnterpriseId(@RequestBody Map<String,Object> interviewEnterpriseData){
        return interviewService.getInterviewInfo(interviewEnterpriseData.get("interviewEnterprise").toString());
    }

    @PostMapping("insertInterview")
    @ResponseBody
    public DataModel insertInterview(@RequestBody Map<String,Object>data){
        System.out.println(data);
        Interview interviewNew = new Interview();
        interviewNew.setInterviewLocation((String) data.get("interviewLocation"));
        interviewNew.setInterviewDate(Date.valueOf((String) data.get("interviewDate")));
        interviewNew.setInterviewTime(Time.valueOf((String) data.get("interviewTime")));
        interviewNew.setInterviewOccupation(Integer.parseInt(data.get("interviewOccupation").toString()));
        interviewNew.setInterviewEnterprise(Integer.parseInt(data.get("interviewEnterprise").toString()));
        interviewNew.setInterviewSeeker(Integer.parseInt(data.get("interviewSeeker").toString()));
        int del = interviewService.insertInterview(interviewNew);
        DataModel model = new DataModel();
        if(del>0){
            model.setCode(0);
        }else {
            model.setCode(-1);
        }
        return model;
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public DataModel deleteById(String interviewId){
        int del = interviewService.deleteById(interviewId);
        DataModel model = new DataModel();
        if (del>0){
            model.setCode(0);
        }else {
            model.setCode(-1);
        }
        return model;
    }

    @PostMapping("updateInterview")
    @ResponseBody
    public DataModel updateInterview(@RequestBody Map<String, Object> data){
        System.out.println(data);
        Interview interview = new Interview();
        interview.setInterviewId((Integer) data.get("interviewId"));
        interview.setInterviewLocation((String) data.get("interviewLocation"));
        interview.setInterviewDate(Date.valueOf((String) data.get("interviewDate")));
        interview.setInterviewTime(Time.valueOf((String) data.get("interviewTime")));
        interview.setInterviewOccupation((Integer) data.get("interviewOccupation"));
        interview.setInterviewEnterprise((Integer) data.get("interviewEnterprise"));
        interview.setInterviewSeeker((Integer) data.get("interviewSeeker"));
        int del = interviewService.updateInterview(interview);
        DataModel model = new DataModel();
        if (del>0){
            model.setCode(0);
        }else {
            model.setCode(-1);
        }
        return model;
    }

}
